//
//  UIView+SunLayout.m
//  test
//
//  Created by sunshine on 16/10/8.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "UIViewSun.h"
#import <objc/runtime.h>
static const void *ltArrKey = &ltArrKey;
@implementation UIView (Sun)
@dynamic ltArr;
#pragma mark sun
- (float)sun_getTop{
    return self.frame.origin.y;
}
- (float)sun_getLeft{
    return self.frame.origin.x;
}
- (float)sun_getBottom{
    return self.frame.origin.y + self.frame.size.height;
}
- (float)sun_getRight{
    return self.frame.origin.x + self.frame.size.width;
}
- (float)sun_getWidth{
    return self.frame.size.width;
}
- (float)sun_getHeight{
    return self.frame.size.height;
}
- (float)sun_getCenterX{
    return [self sun_getLeft] + [self sun_getWidth] / 2.0;
}
- (float)sun_getCenterY{
    return [self sun_getTop] + [self sun_getHeight] / 2.0;
}
#pragma mark frame
- (void)fm_XcenterTo:(UIView *)view{
    if (view == nil) {
        view = self.superview;
    }
    CGRect frame = self.frame;
    frame.origin.x = CGRectGetMinX(view.frame) + (CGRectGetWidth(view.frame) - CGRectGetWidth(frame)) / 2;
    self.frame = frame;
}
- (void)fm_YcenterTo:(UIView *)view{
    if (view == nil) {
        view = self.superview;
    }
    CGRect frame = self.frame;
    frame.origin.y = CGRectGetMinY(view.frame) + (CGRectGetHeight(view.frame) - CGRectGetHeight(frame)) / 2;
    self.frame = frame;
}
- (void)fm_XYcenterTo:(UIView *)view{
    [self fm_XcenterTo:view];
    [self fm_YcenterTo:view];
}
- (void)fm_widthRelyHeight:(CGFloat)size{
    CGRect frame = self.frame;
    frame.size.width = frame.size.height * size;
    self.frame = frame;
}
- (void)fm_heightRelyWidth:(CGFloat)size{
    CGRect frame = self.frame;
    frame.size.height = frame.size.width * size;
    self.frame = frame;
}
- (void)fm_setWidth:(float)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
- (void)fm_setHeight:(float)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
- (void)fm_setSize:(CGSize)size{
    CGRect frame = self.frame;
    frame.size.width = size.width;
    frame.size.height = size.height;
    self.frame = frame;
}
- (void)fm_setTop:(float)top to:(UIView *)view{
    CGRect frame = self.frame;
    frame.origin.y = view?[view sun_getBottom] + top:top;
    self.frame = frame;
}
- (void)fm_setLeft:(float)left to:(UIView *)view{
    CGRect frame = self.frame;
    frame.origin.x = view?[view sun_getRight] + left:left;
    self.frame = frame;
}
- (void)fm_setBottom:(float)bottom to:(UIView *)view{
    CGRect frame = self.frame;
    frame.origin.y = view?[view sun_getTop] - bottom - frame.size.height:[self.superview sun_getHeight] - bottom - frame.size.height;
    self.frame = frame;
}
- (void)fm_setRight:(float)right to:(UIView *)view{
    CGRect frame = self.frame;
    frame.origin.x = view?[view sun_getLeft] - right - frame.size.width:[self.superview sun_getWidth] - right - frame.size.width;
    self.frame = frame;
}
- (void)fm_setLeft:(float)left to:(UIView*)LView Right:(float)right to:(UIView*)RView{
    CGRect frame = self.frame;
    frame.origin.x = LView?[LView sun_getRight] + left:left;
    frame.size.width = RView?[RView sun_getLeft] - frame.origin.x - right:[self.superview sun_getWidth] - frame.origin.x - right;
    self.frame = frame;
}
- (void)fm_setTop:(float)top to:(UIView*)TView Bottom:(float)bottom to:(UIView*)BView{
    CGRect frame = self.frame;
    frame.origin.y = TView?[TView sun_getBottom] + top:top;
    frame.size.height = BView?[BView sun_getTop] - frame.origin.y - bottom:[self.superview sun_getHeight] - frame.origin.y - bottom;
    self.frame = frame;
}
- (void)fm_Bottom:(SunEdge)edges to:(UIView *)view{
    float left = edges.one;
    float bottom = edges.two;
    float right = edges.three;
    float size = edges.four;
    [self fm_setLeft:left to:view Right:right to:view];
    if (size > 0) {
        [self fm_setHeight:size];
    }else{
        [self fm_heightRelyWidth:-size];
    }
    [self fm_setBottom:bottom to:view];
}
- (void)fm_Right:(SunEdge)edges to:(UIView *)view{
    float top = edges.one;
    float bottom = edges.two;
    float right = edges.three;
    float size = edges.four;
    [self fm_setTop:top to:view Bottom:bottom to:view];
    if (size > 0) {
        [self fm_setWidth:size];
    }else{
        [self fm_widthRelyHeight:-size];
    }
    [self fm_setRight:right to:view];
}
- (void)fm_Top:(SunEdge)edges to:(UIView *)view{
    float top = edges.one;
    float left = edges.two;
    float right = edges.three;
    float size = edges.four;
    [self fm_setLeft:left to:view Right:right to:view];
    if (size > 0) {
        [self fm_setHeight:size];
    }else{
        [self fm_heightRelyWidth:-size];
    }
    [self fm_setTop:top to:view];
}
- (void)fm_Left:(SunEdge)edges to:(UIView *)view{
    float top = edges.one;
    float left = edges.two;
    float bottom = edges.three;
    float size = edges.four;
    [self fm_setTop:top to:view Bottom:bottom to:view];
    if (size > 0) {
        [self fm_setWidth:size];
    }else{
        [self fm_widthRelyHeight:-size];
    }
    [self fm_setLeft:left to:view];
}
- (void)fm_All:(SunEdge)edges{
    float top = edges.one;
    float left = edges.two;
    float bottom = edges.three;
    float right = edges.four;
    CGRect frame = self.frame;
    frame.origin.x = left;
    frame.origin.y = top;
    frame.size.width = CGRectGetWidth(self.superview.frame) - left - right;
    frame.size.height = CGRectGetHeight(self.superview.frame) - bottom - top;
    self.frame = frame;
}
#pragma mark autolayout
- (NSMutableArray *)ltArr{
    return objc_getAssociatedObject(self, ltArrKey);
}
- (void)initltArr:(NSMutableArray *)ltArr{
    objc_setAssociatedObject(self, ltArrKey, ltArr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)autolayout{
    if (self.translatesAutoresizingMaskIntoConstraints) {
        self.translatesAutoresizingMaskIntoConstraints = NO;
    }
    if (!self.ltArr) {
        [self initltArr:[[NSMutableArray alloc]init]];
    }
}
- (void)lt_removeAllCons{
    for (id cons in self.ltArr) {
        if ([cons isKindOfClass:[NSArray class]]) {
            [self.superview removeConstraints:cons];
        }else{
            [self.superview removeConstraint:cons];
        }
    }
    [self.ltArr removeAllObjects];
}
- (void)changeSize:(float)size save:(BOOL)save{
    [self lt_setSize:CGSizeMake([self sun_getWidth] * size, [self sun_getHeight] * size) save:save];
}
- (void)lt_layoutView:(UIView *)view side:(SunSide)side to:(UIView *)tView toSide:(SunSide)tSide withSize:(float)size offset:(float)offset save:(BOOL)save{
    [self autolayout];
    NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:view attribute:(NSLayoutAttribute)side relatedBy:NSLayoutRelationEqual toItem:tView?tView:self.superview attribute:(NSLayoutAttribute)tSide multiplier:size constant:offset];
    [self.superview addConstraint:con];
    if (save) {
        [self.ltArr addObject:con];
    }
}
- (void)lt_XcenterTo:(UIView *)view save:(BOOL)save{
    [self lt_layoutView:self side:SunCenterX to:view?view:self.superview toSide:SunCenterX withSize:1 offset:0 save:save];
}
- (void)lt_YcenterTo:(UIView *)view save:(BOOL)save{
    [self lt_layoutView:self side:SunCenterY to:view?view:self.superview toSide:SunCenterY withSize:1 offset:0 save:save];
}
- (void)lt_XYcenterTo:(UIView *)view save:(BOOL)save{
    [self lt_XcenterTo:view save:save];
    [self lt_YcenterTo:view save:save];
}
- (void)lt_bottomEqual:(UIView *)view save:(BOOL)save{
    [self lt_layoutView:self side:SunBottom to:view?view:self.superview toSide:SunBottom withSize:1 offset:0 save:save];
}
- (void)lt_topEqual:(UIView *)view save:(BOOL)save{
    [self lt_layoutView:self side:SunTop to:view?view:self.superview toSide:SunTop withSize:1 offset:0 save:save];
}
- (void)lt_leftEqual:(UIView *)view save:(BOOL)save{
    [self lt_layoutView:self side:SunLeft to:view?view:self.superview toSide:SunLeft withSize:1 offset:0 save:save];
}
- (void)lt_rightEqual:(UIView *)view save:(BOOL)save{
    [self lt_layoutView:self side:SunRight to:view?view:self.superview toSide:SunRight withSize:1 offset:0 save:save];
}
- (void)lt_widthRelyHeight:(CGFloat)size save:(BOOL)save{
    [self lt_layoutView:self side:SunWidth to:self toSide:SunHeight withSize:size offset:0 save:save];
}
- (void)lt_heightRelyWidth:(CGFloat)size save:(BOOL)save{
    [self lt_layoutView:self side:SunHeight to:self toSide:SunWidth withSize:size offset:0 save:save];
}
- (void)lt_setWidth:(float)width save:(BOOL)save{
    [self autolayout];
    NSArray *con = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[view(%f)]",width] options:0 metrics:nil views:@{@"view":self}];
    [self.superview addConstraints:con];
    if (save) {
        [self.ltArr addObject:con];
    }
}
- (void)lt_setHeight:(float)height save:(BOOL)save{
    [self autolayout];
    NSArray *con = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view(%f)]",height] options:0 metrics:nil views:@{@"view":self}];
    [self.superview addConstraints:con];
    if (save) {
        [self.ltArr addObject:con];
    }
}
- (void)lt_setSize:(CGSize)size save:(BOOL)save{
    [self autolayout];
    NSArray *conH = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[view(%f)]",size.width] options:0 metrics:nil views:@{@"view":self}];
    NSArray *conV = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view(%f)]",size.height] options:0 metrics:nil views:@{@"view":self}];
    [self.superview addConstraints:conH];
    [self.superview addConstraints:conV];
    if (save) {
        [self.ltArr addObject:conH];
        [self.ltArr addObject:conV];
    }
}
- (void)lt_setTop:(float)top to:(UIView *)view save:(BOOL)save{
    [self autolayout];
    NSString *topF = @"|";
    NSMutableDictionary *views = [NSMutableDictionary dictionaryWithDictionary:@{@"view":self}];
    if (view) {
        topF = @"[tview]";
        [views addEntriesFromDictionary:@{@"tview":view}];
    }
    NSString *format = [[@"V:" stringByAppendingString:topF] stringByAppendingString:[NSString stringWithFormat:@"-%f-[view]",top]];
    NSArray *con = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [self.superview addConstraints:con];
    if (save) {
        [self.ltArr addObject:con];
    }
}
- (void)lt_setLeft:(float)left to:(UIView *)view save:(BOOL)save{
    [self autolayout];
    NSString *leftF = @"|";
    NSMutableDictionary *views = [NSMutableDictionary dictionaryWithDictionary:@{@"view":self}];
    if (view) {
        leftF = @"[lview]";
        [views addEntriesFromDictionary:@{@"lview":view}];
    }
    NSString *format = [[@"H:" stringByAppendingString:leftF] stringByAppendingString:[NSString stringWithFormat:@"-%f-[view]",left]];
    NSArray *con = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [self.superview addConstraints:con];
    if (save) {
        [self.ltArr addObject:con];
    }
}
- (void)lt_setBottom:(float)bottom to:(UIView *)view save:(BOOL)save{
    [self autolayout];
    NSString *bottomF = @"|";
    NSMutableDictionary *views = [NSMutableDictionary dictionaryWithDictionary:@{@"view":self}];
    if (view) {
        bottomF = @"[bview]";
        [views addEntriesFromDictionary:@{@"bview":view}];
    }
    NSString *format = [[@"V:" stringByAppendingString:[NSString stringWithFormat:@"[view]-%f-",bottom]] stringByAppendingString:bottomF];
    NSArray *con = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [self.superview addConstraints:con];
    if (save) {
        [self.ltArr addObject:con];
    }
}
- (void)lt_setRight:(float)right to:(UIView *)view save:(BOOL)save{
    [self autolayout];
    NSString *rightF = @"|";
    NSMutableDictionary *views = [NSMutableDictionary dictionaryWithDictionary:@{@"view":self}];
    if (view) {
        rightF = @"[rview]";
        [views addEntriesFromDictionary:@{@"rview":view}];
    }
    NSString *format = [[@"H:" stringByAppendingString:[NSString stringWithFormat:@"[view]-%f-",right]] stringByAppendingString:rightF];
    NSArray *con = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [self.superview addConstraints:con];
    if (save) {
        [self.ltArr addObject:con];
    }
}
- (void)lt_setLeft:(float)left to:(UIView*)LView Right:(float)right to:(UIView*)RView save:(BOOL)save{
    [self autolayout];
    NSString *leftF = @"|";
    NSString *rightF = @"|";
    NSMutableDictionary *views = [NSMutableDictionary dictionaryWithDictionary:@{@"view":self}];
    if (LView) {
        leftF = @"[LView]";
        [views addEntriesFromDictionary:@{@"LView":LView}];
    }
    if (RView) {
        rightF = @"[RView]";
        [views addEntriesFromDictionary:@{@"RView":RView}];
    }
    NSString *format = [[[@"H:" stringByAppendingString:leftF]stringByAppendingString:[NSString stringWithFormat:@"-%f-[view]-%f-",left,right]]stringByAppendingString:rightF];
    NSArray *conH = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [self.superview addConstraints:conH];
    if (save) {
        [self.ltArr addObject:conH];
    }
}
- (void)lt_setTop:(float)top to:(UIView*)TView Bottom:(float)bottom to:(UIView*)BView save:(BOOL)save{
    [self autolayout];
    NSString *topF = @"|";
    NSString *bottomF = @"|";
    NSMutableDictionary *views = [NSMutableDictionary dictionaryWithDictionary:@{@"view":self}];
    if (TView) {
        topF = @"[TView]";
        [views addEntriesFromDictionary:@{@"TView":TView}];
    }
    if (BView) {
        bottomF = @"[BView]";
        [views addEntriesFromDictionary:@{@"BView":BView}];
    }
    NSString *format = [[[@"V:" stringByAppendingString:topF]stringByAppendingString:[NSString stringWithFormat:@"-%f-[view]-%f-",top,bottom]]stringByAppendingString:bottomF];
    NSArray *conV = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [self.superview addConstraints:conV];
    if (save) {
        [self.ltArr addObject:conV];
    }
}
- (void)lt_All:(SunEdge)edges save:(BOOL)save{
    [self autolayout];
    float top = edges.one;
    float left = edges.two;
    float bottom = edges.three;
    float right = edges.four;
    NSArray *conH = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-%f-[view]-%f-|",left,right] options:0 metrics:nil views:@{@"view":self}];
    NSArray *conV = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-%f-[view]-%f-|",top,bottom] options:0 metrics:nil views:@{@"view":self}];
    [self.superview addConstraints:conH];
    [self.superview addConstraints:conV];
    if (save) {
        [self.ltArr addObject:conH];
        [self.ltArr addObject:conV];
    }
}
- (void)lt_Top:(SunEdge)edges to:(UIView *)view save:(BOOL)save{
    float top = edges.one;
    float left = edges.two;
    float right = edges.three;
    float size = edges.four;
    [self lt_setLeft:left to:nil Right:right to:nil save:save];
    [self lt_setTop:top to:view save:save];
    if (size > 0) {
        [self lt_setHeight:size save:save];
    }else{
        [self lt_heightRelyWidth:-size save:save];
    }
}
- (void)lt_Left:(SunEdge)edges to:(UIView *)view save:(BOOL)save{
    float top = edges.one;
    float left = edges.two;
    float bottom = edges.three;
    float size = edges.four;
    [self lt_setTop:top to:nil Bottom:bottom to:nil save:save];
    [self lt_setLeft:left to:view save:save];
    if (size > 0) {
        [self lt_setWidth:size save:save];
    }else{
        [self lt_widthRelyHeight:-size save:save];
    }
}
- (void)lt_Bottom:(SunEdge)edges to:(UIView *)view save:(BOOL)save{
    float left = edges.one;
    float bottom = edges.two;
    float right = edges.three;
    float size = edges.four;
    [self lt_setLeft:left to:nil Right:right to:nil save:save];
    [self lt_setBottom:bottom to:view save:save];
    if (size > 0) {
        [self lt_setHeight:size save:save];
    }else{
        [self lt_heightRelyWidth:-size save:save];
    }
}
- (void)lt_Right:(SunEdge)edges to:(UIView *)view save:(BOOL)save{
    float top = edges.one;
    float bottom = edges.two;
    float right = edges.three;
    float size = edges.four;
    [self lt_setTop:top to:nil Bottom:bottom to:nil save:save];
    [self lt_setRight:right to:view save:save];
    if (size > 0) {
        [self lt_setWidth:size save:save];
    }else{
        [self lt_widthRelyHeight:-size save:save];
    }
}
@end
